﻿using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace PersonEntity
{
    public class DS_NET_Client : iDS
    {
        public static TcpClient clientSocket = new TcpClient();
        public static NetworkStream serverStream = default(NetworkStream);
        IFormatter formatter = new BinaryFormatter();

        public DS_NET_Client()
        {
            //TryConnect();
        }

        public void TryConnect()
        {
            if (clientSocket.Connected == false)
            {
                clientSocket = new TcpClient();
                clientSocket.SendBufferSize = 20000;
                clientSocket.Connect("127.0.0.1", 4040);
                serverStream = clientSocket.GetStream();
            }
            
        }
        public override void Create(Person p)
        {
            TryConnect();
            string strP = "create^" ;
            byte[] outStream = System.Text.Encoding.UTF8.GetBytes(strP + "\n");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            Wait();
            //Receive Data
            byte[] bytesFrom = new byte[10025];
            serverStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
            clientSocket.Close();
        }

        public override Person Read(int ID)
        {
            TryConnect();
            //Sending request
            Person p = null;
            string strP = "read^" + ID;
            byte[] outStream = System.Text.Encoding.UTF8.GetBytes(strP + "\n");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            //wainit data
            Wait();

            //Receive Data
            byte[] bytesFrom = new byte[10025];
            string dataFromServer = null;
            serverStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
            dataFromServer = System.Text.Encoding.UTF8.GetString(bytesFrom);
            dataFromServer = dataFromServer.Substring(0, dataFromServer.IndexOf("\n"));

            //Parse Data
            string temp = dataFromServer;
            string[] tempArr = temp.Split(',');
            if (tempArr.Length > 3)
            {
                p = new Person();
               
            }
            clientSocket.Close();
            return p;
            
        }

        public override void Update(Person p)
        {
            TryConnect();
            //send command
            string strP = "update^";
            byte[] outStream = System.Text.Encoding.UTF8.GetBytes(strP + "\n");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
            Wait();
            //Receive Data
            byte[] bytesFrom = new byte[10025];
            serverStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

            //send object
            formatter.Serialize(serverStream, p);
            Wait();
            //Receive Data
            bytesFrom = new byte[10025];
            serverStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
            clientSocket.Close();
        }

        public override void Delete(int ID)
        {
            TryConnect();

            string strP = "delete^" + ID;
            byte[] outStream = System.Text.Encoding.UTF8.GetBytes(strP + "\n");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            Wait();
            //Receive Data
            byte[] bytesFrom = new byte[10025];
            serverStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
            clientSocket.Close();
        }

        public override List<Person> Load()
        {
            TryConnect();
            //Sending request
            string strP = "load";
            byte[] outStream = System.Text.Encoding.UTF8.GetBytes(strP + "\n");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            //wainit data
            Wait();

            //Receive Data            
            object obj = formatter.Deserialize(serverStream);            
            clientSocket.Close();

            return (List<Person>)obj;
        }

       

        void Wait()
        {
            int count = 0;
            while (!serverStream.DataAvailable)
            {
                Thread.Sleep(100);
                count++;
                if (count == 100)
                {
                    break;
                }
            }
        }
    }
}
